package com.guet.careapp.service.impl;

import com.guet.careapp.controller.rest.common.errorCode.Error;
import com.guet.careapp.controller.rest.common.errorCode.ErrorCode;
import com.guet.careapp.dao.IUserGuardianInfoDao;
import com.guet.careapp.dao.IUserRelationDao;
import com.guet.careapp.pojo.TUserGuardianInfo;
import com.guet.careapp.pojo.TUserRelation;
import com.guet.careapp.pojo.TUserUnderGuardiansInfo;
import com.guet.careapp.service.*;
import com.guet.careapp.service.base.ServiceSupportImpl;
import com.guet.careapp.utils.TimeHelper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Created by LXM on 2017/10/27.
 *
 * @author LXM
 * @version 1.0
 * @description
 * @datetime 17:19
 */
@Service("relation")
public class UserRelationServiceImpl extends ServiceSupportImpl<TUserRelation> implements
    IUserRelationService {
//  protected static final org.slf4j.Logger LOG = LoggerFactory.getLogger(UserRelationServiceImpl.class);
  @Autowired
  IUserGuardianInfoDao userGuardianInfoDao;

  @Autowired
  private IInstructionService instructionService;

  @Autowired
  private IUserGuardianInfoService guardianInfoService;


  @Autowired
  private IUserUnderGuardiansInfoService underGuardiansInfoService;

  @Autowired
  private IUserRelationDao userRelationDao;



  @Override
  public ErrorCode bindingRelation(Map map) {
    return null;
  }

  @Override
  public String unbindingRelation(Map map) {
    return null;
  }

  @Override
  public String search(Map map, int type) {
    return null;
  }

  @Override
  public Error assignment(int gid, int ugid, int other_id) {

    Map<String, Object> map = new HashMap<>();
    map.put("g_info_id", gid);
    map.put("ug_info_id", ugid);
    TUserRelation relation = findByField(map);
    TUserGuardianInfo guardianInfo = userGuardianInfoDao.findById(other_id);
    if (relation == null || guardianInfo == null) {
      return Error.NotFond;
    }
    relation.setGInfoId(guardianInfo);
    relation.setEstablishTime(TimeHelper.getTime());
    try {
      update(relation);
      return Error.Success;
    } catch (Exception e) {
      return Error.Unkonwn;
    }
  }

  @Override
  public boolean checkRelation(int gid, int ugid) {
    TUserGuardianInfo guardian;
    TUserUnderGuardiansInfo userUnderGuardian;
    Map<String, Object> fieldMap;

    try
    {
      guardian = guardianInfoService.findById(gid);
      userUnderGuardian = underGuardiansInfoService.findById(ugid);
      if ( null== guardian || null == userUnderGuardian)
        return false;
      fieldMap = new HashMap<>();
      fieldMap.put("g_info_id", gid);
      fieldMap.put("ug_info_id",ugid);
      List<TUserRelation> relations =userRelationDao.findAllByField(fieldMap);
      if (relations.size()==0)
        return false;
//      LOG.info("验证失败，关系不存在");
    }
    catch (Exception e)
    {
//      LOG.info("验证失败，关系不存在");
//      LOG.error(e.toString());
      return false;
    }
    return true;
  }


}
